<?php
namespace Admin\Controller;
use Tools\AdminController,Model\News_classModel,
Model\News_contentsModel;
class NewsclassController extends AdminController {
    public function showlist(){
      //展示新闻栏目
      //  $info = D('News_class');
      $info  = new News_classModel();
      $flist = $info-> order('sort asc') -> select();

      $this -> assign('flist',$flist); 
      $this -> display();
    }
    
    
    function delete($id){
         //删除di为$id的新闻栏目
         //将栏目包含的新闻状态改为0
         $ncls  =  new News_classModel();
         $cts   =  new News_contentsModel();
         
         $res   =  $ncls  -> delete($id);      
         if($res) { 
             $cts  -> where("n_id=$id");
             $cts->delete();
             $this -> redirect('showlist','',2,'删除成功');
         } else {
             $this -> redirect('showlist','',2,'栏目删除失败');
         }
    }
    
   
    function add(){
        //添加栏目
        //栏目信息
        $ncls  = new News_classModel();

       
        //收集栏目信息，
        if(!empty($_POST)) {
            $shu = $ncls -> create($_POST);
            if($shu) {
                if($ncls -> add($shu)){
                     $this -> redirect('showlist');
                }else{
                    echo "添加失败";
                }
            }else {
                $error = $ncls -> getError();
                $this  -> alert($error);  
            }
        }
        
        $this -> display();
    }
    
    
    function xiugai($id){
        //修改栏目信息
        $ncls = new News_classModel();
        $list = $ncls -> find($id);
        
        //接收并更新
        if(!empty($_POST)) {
            $shu = $ncls -> create();
            if($shu) {
                if($ncls -> save($shu)){
                    $this -> redirect('showlist');
                }else{
                    $this -> redirect('showlist');
                }
            }else {
                $error = $ncls -> getError();
                $this  -> alert($error);  
            }
        }
        
        $this -> assign('list',$list);
        $this -> display();
    }
    
    
    function sort_change($id,$sort){
        $ncmd = new News_classModel();
        
        $up['id']   = $id;
        $up['sort'] = $sort;
        
        $shu = $ncmd -> create($up);
        if($shu){
            $ncmd -> save($shu);
        }else{
            echo $ncmd -> getError();
        }              
    }
    
}